package cn.itcast.web.controller.system;


import cn.hutool.core.util.IdUtil;
import cn.itcast.domain.system.Notice;
import cn.itcast.domain.system.NoticeExample;
import cn.itcast.domain.system.User;
import cn.itcast.domain.vo.PersonalVo;
import cn.itcast.service.system.NoticeService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private UserService userService;


    /**
     * 转到反馈信息弹窗
     *
     * @return
     */
    @RequestMapping("/toFeedback")
    public String feedback() {
        User loginUser = getLoginUser();
        request.setAttribute("loginUser", loginUser);
        return "system/notice/feedback";
    }

    /**
     * 点击提交反馈信息
     *
     * @param notice
     * @return
     */
    @RequestMapping("/feedback")
    public String Message(Notice notice) {
        //记录发送者ID
        notice.setSendemail(getLoginUser().getEmail());
        notice.setReportid(IdUtil.simpleUUID());
        //记录发送时间
        notice.setSendtime(new Date());
        //设置通知为未读

        try {
            PersonalVo personalById = userService.findPersonalById(getLoginUser().getId());
            String iconUrl = personalById.getIconUrl();
            notice.setSendphoto(iconUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        notice.setSendname(getLoginUser().getUserName());
        notice.setFlag(0);
        noticeService.save(notice);
        return "system/notice/feedbackSuccess";
    }

    /**
     * 小图标获取邮件信息   AJAX
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/getAJAXEmail")
    @ResponseBody
    public List<Notice> getEmail() throws Exception {
        //查询关于自己的信息
        //如果为0就是管理员
        List<Notice> noticeList = null;
        if (getLoginUser().getDegree() == 0) {
            //查询关于意见反馈的信息
            NoticeExample noticeExample = new NoticeExample();
            noticeExample.createCriteria().andAcceptemailEqualTo("110@saas.com").andStateEqualTo(0).andFlagEqualTo(0);
            noticeList = noticeService.findAll(noticeExample);
        }
        //查询普通用户信息
        NoticeExample emailExample = new NoticeExample();
        //传入登录人的ID
        emailExample.createCriteria().andAcceptemailEqualTo(getLoginUser().getEmail()).andStateEqualTo(0).andFlagEqualTo(0);
        List<Notice> message = noticeService.findAll(emailExample);
        if (noticeList != null && message != null) {
            noticeList.addAll(message);
            return noticeList;
        }


        if (noticeList == null && message != null) {
            noticeList = message;
            return noticeList;
        }
        return null;


    }

    /**
     * 点击查询所有 收件箱
     * 转发至邮箱界面
     * 查询邮箱
     *
     * @return
     */
    @RequestMapping("/toReceive")
    public String toReceive() {
        List<Notice> noticeList = null;
        List<Notice> noRead = null;
        int num = 0;
        //如果当前为管理员,则显示反馈邮箱
        if (getLoginUser().getDegree() == 0) {
            NoticeExample noticeExample = new NoticeExample();
            noticeExample.createCriteria().andStateEqualTo(0).andAcceptemailEqualTo("110@saas.com");
            noticeList = noticeService.findAll(noticeExample);
            NoticeExample noReadExample = new NoticeExample();
            noReadExample.createCriteria().andAcceptemailEqualTo("110@saas.com").andStateEqualTo(0).andFlagEqualTo(0);
            noRead = noticeService.findAll(noReadExample);
        }
        //如果为个人用户显示个人邮箱信息
        NoticeExample noticeExample = new NoticeExample();
        noticeExample.createCriteria().andStateEqualTo(0).andAcceptemailEqualTo(getLoginUser().getEmail());
        List<Notice> emailList = noticeService.findAll(noticeExample);

        if (emailList != null && noticeList != null) {
            noticeList.addAll(emailList);
        }
        if (noticeList == null && emailList != null) {
            noticeList = emailList;
        }
        NoticeExample noReadExample = new NoticeExample();
        noReadExample.createCriteria().andStateEqualTo(0).andAcceptemailEqualTo(getLoginUser().getEmail()).andFlagEqualTo(0);
        List<Notice> userNoRead = noticeService.findAll(noReadExample);
        if (userNoRead != null && noRead != null) {
            noRead.addAll(userNoRead);
            num = noRead.size();
        }
        if (userNoRead == null && noRead != null) {
            noRead = userNoRead;
            num = noRead.size();
        }
        request.setAttribute("num", num);
        request.setAttribute("noticeList", noticeList);

        return "system/notice/email";
    }

    /**
     * 查看未读邮件
     *
     * @return
     */
    @RequestMapping("/toEmailBox")
    public String toEmailBox() {

        List<Notice> noRead = null;
        //如果当前为管理员,则显示反馈邮箱
        if (getLoginUser().getDegree() == 0) {

            NoticeExample noReadExample = new NoticeExample();
            noReadExample.createCriteria().andStateEqualTo(0).andAcceptemailEqualTo("110@saas.com").andFlagEqualTo(0);
            noRead = noticeService.findAll(noReadExample);
        }
        //如果为个人用户显示个人邮箱信息
        NoticeExample noReadExample = new NoticeExample();
        noReadExample.createCriteria().andStateEqualTo(0).andAcceptemailEqualTo(getLoginUser().getEmail()).andFlagEqualTo(0);
        List<Notice> all = noticeService.findAll(noReadExample);
        if (noRead != null && all != null) {
            noRead.addAll(all);
            int num = noRead.size();
            request.setAttribute("num", num);
            request.setAttribute("noticeList", noRead);
        }
        if (noRead == null && all != null) {
            int num = all.size();
            request.setAttribute("num", num);
            request.setAttribute("noticeList", all);
        }


        return "system/notice/email";
    }

    /**
     * 发件箱
     *
     * @return
     */
    @RequestMapping("/toSendBox")
    public String toSendBox() {
        NoticeExample noticeExample = new NoticeExample();
        noticeExample.createCriteria().andStateEqualTo(0).andSendemailEqualTo(getLoginUser().getEmail());
        List<Notice> sendList = noticeService.findAll(noticeExample);
        if (sendList != null) {
//            int num = sendList.size();
//            request.setAttribute("num", num);
            request.setAttribute("sendList", sendList);
        }


        return "system/notice/send";
    }

    /**
     * 点击小图标到阅读单个邮件信息
     * 点击阅读邮件信息
     * /notice/person.do?email=email{}
     */

    @RequestMapping("/oneEmail")
    public String oneEmail(String reportid) {
        System.out.println(reportid);
        //根据邮箱ID查询信息
        Notice oneEmail = noticeService.findById(reportid);
        //点击读取邮件,设置为已读
        oneEmail.setFlag(1);
        noticeService.update(oneEmail);

        request.setAttribute("oneEmail", oneEmail);
        return "system/notice/read-mail";
    }

    /**
     * 发邮件
     *
     * @param notice
     * @return
     */
    @RequestMapping("/toEmail")
    public String toEmail(Notice notice) {
        notice.setSendemail(getLoginUser().getEmail());
        notice.setReportid(IdUtil.simpleUUID());
        //记录发送时间
        notice.setSendtime(new Date());
        //设置通知为未读
        notice.setSendname(getLoginUser().getUserName());
        notice.setFlag(0);

        try {
            PersonalVo personalById = userService.findPersonalById(getLoginUser().getId());
            String iconUrl = personalById.getIconUrl();
            notice.setSendphoto(iconUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        noticeService.save(notice);
        return "redirect:/notice/toEmailBox.do";
    }

    /**
     * to/notice/compose.do 写邮件
     */
    @RequestMapping("/toCompose")
    public String toCompose() {
        return "system/notice/compose";
    }

    /**
     * 写邮件-->点击发送邮件
     */
    @RequestMapping("/sendEmail")
    public String sendEmail(Notice notice) {
        notice.setSendemail(getLoginUser().getEmail());
        notice.setReportid(IdUtil.simpleUUID());
        //记录发送时间

        notice.setSendtime(new Date());
        notice.setSendname(getLoginUser().getUserName());
        notice.setFlag(0);
        noticeService.save(notice);
        return "redirect:/notice/toSendBox.do";
    }

    /**
     * 回复邮件
     */
    @RequestMapping("/toReply")
    public String toReply(String sendemail) {
        request.setAttribute("replyemail", sendemail);

        return "redirect:/notice/toSendBox.do";
    }

    /**
     * 点击删除-->收件箱删除
     * deleteEmail
     */
    @RequestMapping("/deleteEmail")
    public String deleteEmail(String id) {
        NoticeExample noticeExample = new NoticeExample();
        System.out.println();
        Notice notice = noticeService.findById(id);
        notice.setState(1);
        noticeService.update(notice);

        return "redirect:/notice/toReceive.do";
    }

    /**
     * 发件箱删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteEmailSend")
    public String deleteEmailSend(String id) {
        NoticeExample noticeExample = new NoticeExample();
        System.out.println();
        Notice notice = noticeService.findById(id);
        notice.setState(1);
        noticeService.update(notice);

        return "redirect:/notice/toSendBox.do";
    }

    /**
     * 点击垃圾箱
     */
    @RequestMapping("/lajixiang")
    public String lajixiang() {
        List<Notice> garbageList = null;
        NoticeExample noticeExample = new NoticeExample();
        noticeExample.createCriteria().andSendemailEqualTo(getLoginUser().getEmail()).andStateEqualTo(1);
        garbageList = noticeService.findAll(noticeExample);
        NoticeExample noticeExample1 = new NoticeExample();
        noticeExample1.createCriteria().andAcceptemailEqualTo(getLoginUser().getEmail()).andStateEqualTo(1);
        List<Notice> list2 = noticeService.findAll(noticeExample1);
        if (garbageList != null && list2 != null) {
            garbageList.addAll(list2);
            request.setAttribute("garbageList", garbageList);
        } else if (garbageList == null && list2 != null) {
            garbageList = list2;
            request.setAttribute("garbageList", garbageList);
        }

        return "system/notice/garbage";
    }

    //嘎嘎嘎嘎嘎

    /**
     * 垃圾箱删除
     */
    @RequestMapping("/garbageDelete")
    public String garbageDelete(String id) {


        Notice notice = noticeService.findById(id);
        notice.setState(2);
        noticeService.update(notice);

        return "redirect:/notice/lajixiang.do";
    }
}